﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.BU;
using Com.DA;

namespace Mainform
{
    /// <summary>
    /// Interaction logic for ucListInformation.xaml
    /// </summary>
    public partial class ucListInformation : UserControl
    {
        public ucListInformation()
        {
            InitializeComponent();
            LoadLocation();
            bLoadedLocation = true;
        }
        public TreeViewItem tvAir = new TreeViewItem();
        public TreeViewItem tvAlt = new TreeViewItem();
        public int iAlternative = 1;
        public bool bLoadedLocation = false;
        TreeViewItem selectedTVI;

        public void LoadLocation()
        {
            cbxLocation.ItemsSource = null;
            cbxLocation.Items.Clear();
            LibraryManagerInCom.loadLocation();
            cbxLocation.ItemsSource = LibraryManagerInCom.LocationList;
            //foreach (var item in LibraryManagerInCom.LocationList)
            //{
            //    cbxLocation.Items.Add(item);
                
            //}
            cbxLocation.DisplayMemberPath = "location";
            cbxLocation.SelectedIndex = 0;
        }

        //add note to treeview
        public void addAir()
        {
            tvAir.Header = "Air Condition";
            trvList.Items.Add(tvAir);
            tvAlt.Header = "Alternative " + iAlternative;
            tvAir.Items.Add(tvAlt);
            iAlternative++;
        }
        public void addCold()
        {
            TreeViewItem tvCold = new TreeViewItem();
            tvCold.Header = "Cold Storage";
            this.trvList.Items.Add(tvCold);
        }

        // DELEGATE Tạo Mới RooM
        public delegate void GetRoom(string sroomname,string saltname);
        public GetRoom MyRoom;

        public delegate void GetAlt(string saltname);
        public GetAlt MyAlt;

        private void trvList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            int parentindex = trvList.Items.IndexOf(((TreeViewItem)trvList.SelectedItem).Parent);
            int countitem = ((TreeViewItem)trvList.SelectedItem).Items.Count;
            selectedTVI = (TreeViewItem)trvList.SelectedItem;
            if (parentindex == 0)
            {
                cbxDelete.IsEnabled = true;
                if (MyAlt != null)
                {
                    MyAlt(selectedTVI.Header.ToString());
                }
            }
            else
            {
                cbxDelete.IsEnabled = false;
                if (countitem == 0)
                {
                    if (MyRoom != null)
                    {
                        MyRoom(selectedTVI.Header.ToString(), ((TreeViewItem)selectedTVI.Parent).Header.ToString());
                    }
                }
            }
        }

        public delegate void GetLocation(string sOutsideTemperature);
        public GetLocation MyLocation;
        private void cbxLocation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string sOutsideTemperature = (((tb_location)cbxLocation.SelectedItem).average_temperature + 0.125 * ((tb_location)cbxLocation.SelectedItem).max_temperature).ToString();
            if (bLoadedLocation)
            {
                MyLocation(sOutsideTemperature);
            }
        }

        private void cbxDelete_Click(object sender, RoutedEventArgs e)
        {
            tvAir.Items.Remove(selectedTVI);
        }
    }
}
